/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: javaseapp - QrDemo
 * <p>Powered by webrx On 2021-08-16 17:54:44
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class QrDemo {
    public static void main(String[] args) throws WriterException, IOException {
        //二维码 字符串编码为图案
        String name = "qr.png";
        String content = "https://huaban.com/pins/4137294905/";
        int width = 400;
        int height = 400;
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.MARGIN, 2);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream(name));
        MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream(name));
        Runtime run = Runtime.getRuntime();
        run.exec("cmd /k start qr.png");
    }
}
